package com.ayf.payment.game.api.service.sign.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ayf.payment.game.api.config.RequestConfig;
import com.ayf.payment.game.api.config.SystemInitialization;
import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.ayb.SignPayProductDTO;
import com.ayf.payment.game.api.dto.ayf.MerchantTwoAyfDTO;
import com.ayf.payment.game.api.dto.platform.PaymentChannelDTO;
import com.ayf.payment.game.api.dto.sign.*;
import com.ayf.payment.game.api.entity.Merchant;
import com.ayf.payment.game.api.entity.MerchantBank;
import com.ayf.payment.game.api.exception.NullSetException;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.exception.ParameterListException;
import com.ayf.payment.game.api.httpclient.HttpUtils;
import com.ayf.payment.game.api.service.MerchantBankService;
import com.ayf.payment.game.api.service.MerchantService;
import com.ayf.payment.game.api.service.SignPayProductService;
import com.ayf.payment.game.api.utils.FtpUtil;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.ayf.payment.game.api.utils.ValidateUtil;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: zh-liang
 * @Date: 2022-03-11 15:55
 */
@Component("merchantTwoAyfSignHandler")
public class MerchantTwoAyfSignHandlerImpl implements MerchantTwoSignHandler {
    @Resource
    public SystemInitialization systemInitialization;
    @Resource
    private MerchantService merchantService;
    @Resource
    private MerchantBankService merchantBankService;

    @Resource
    private SignPayProductService signPayProductService;


    private static final String PLATFORM_CODE = System.getenv("GAME_AYFPAY_PLATFORM_CODE");

    private String getImgBase64(String traceId, String fileName) {
        try {
            byte[] bytes = FtpUtil.downloadXYFile("", fileName);
            String type = fileName.substring(fileName.lastIndexOf(".") + 1);
            String base64 = Base64.getEncoder().encodeToString(bytes);
            return type + ";" + base64;
        } catch (Exception e) {
            LogPortal.error("[{}]获取身份证图片发生[{}]异常,异常信息为[{}]", e, traceId, e.getClass().getSimpleName(), e.getMessage());
        }
        return null;
    }

    private void checkParameter(MerchantDTO dto, MerchantBank merchantBank) throws ParameterException {
        List<String> msg = new ArrayList<>();
        if (StrUtil.isEmpty(merchantBank.getBankPhone())) {
            msg.add("银行预留手机号 不能为空");
        }
        if (StrUtil.isEmpty(dto.getDetailAddress())) {
            msg.add("详细地址 不能为空");
        }
        if (StrUtil.isNotEmpty(dto.getDetailAddress()) && dto.getDetailAddress().length() < 10) {
            msg.add("详细地址 至少10位");
        }
        if (StrUtil.isEmpty(dto.getIdCardValidityTimeStart())) {
            msg.add("身份证有效期 不能为空");
        }
        if (StrUtil.isEmpty(dto.getIdCardValidityTimeEnd())) {
            msg.add("身份证有效期 不能为空");
        }
        if (StrUtil.isEmpty(dto.getIdCardBackImg())) {
            msg.add("身份证背面照 不能为空");
        }
        if (StrUtil.isEmpty(dto.getIdCardFrontImg())) {
            msg.add("身份证正面照 不能为空");
        }
        if (StrUtil.isEmpty(merchantBank.getAccountName())) {
            msg.add("收款人 不能为空");
        }
        if (StrUtil.isEmpty(merchantBank.getCardNo())) {
            msg.add("银行账号 不能为空");
        }
        if (StrUtil.isEmpty(merchantBank.getBankName())) {
            msg.add("开户银行 不能为空");
        }
        if (StringUtils.isEmpty(dto.getIdCard())) {
            msg.add("身份证号 不能为空");
        }
        if (StringUtils.isNotBlank(dto.getIdCard()) && !IdcardUtil.isValidCard(dto.getIdCard())) {
            msg.add("身份证号 格式错误");
        }
        if (StringUtils.isEmpty(merchantBank.getBankCardImg())) {
            msg.add("银行卡正面照 不能为空");
        }
        if (StringUtils.isEmpty(dto.getBusinessAddressProvinceCode())) {
            msg.add("联系地址-省 不能为空");
        }
        if (StringUtils.isEmpty(dto.getBusinessAddressCityCode())) {
            msg.add("联系地址-市 不能为空");
        }
        if (StringUtils.isEmpty(dto.getBusinessAddressAreaCode())) {
            msg.add("联系地址-区 不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getBankProvinceCode())) {
            msg.add("开户省 不能为空");
        }
        if (StringUtils.isEmpty(merchantBank.getBankCityCode())) {
            msg.add("开户市 不能为空");
        }
        if (CollUtil.isNotEmpty(msg)) {
            throw new ParameterListException(msg);
        }
    }

    @Override
    public MerchantTwoApplyResDTO merchantTwoApply(MerchantTwoApplyReqDTO reqDTO, PaymentChannelDTO paymentChannelDTO) {
        String traceId = SnowFlakeUtils.getTraceId();
        Merchant merchant = new Merchant();
        merchant.setId(reqDTO.getMerchantId());
        MerchantDTO merchantDTO = merchantService.selectOne(merchant);
        MerchantBank merchantBank = merchantBankService.getMerchantBankByMid(reqDTO.getMerchantId());
        SignPayProductDTO signPayProduct = signPayProductService.getByMerchantIdAndPayCode(merchantDTO.getId(),paymentChannelDTO.getCode());
        ValidateUtil.businessValidate(signPayProduct == null || signPayProduct.getCountSignPayProduct() == 0, "已绑定，请勿重复操作");
        checkParameter(merchantDTO, merchantBank);
        Map<String, String> result = new HashMap<>(16);
        Map<String, Object> reqMap = new HashMap<>(16);
        reqMap.put("platformCode", PLATFORM_CODE);
        reqMap.put("merchantId", paymentChannelDTO.getAccountNumber());
        reqMap.put("customerMerchantId", merchantDTO.getId());
        reqMap.put("customerMerchantUserName", merchantBank.getAccountName());
        reqMap.put("phone", merchantBank.getBankPhone());
        reqMap.put("streetAddress", merchantDTO.getDetailAddress());
        reqMap.put("idCardStartDate", merchantDTO.getIdCardValidityTimeStart().replaceAll("-", ""));
        reqMap.put("idCardEndDate", merchantDTO.getIdCardValidityTimeEnd().replaceAll("-", ""));
        reqMap.put("idCardNo", merchantDTO.getIdCard());
        reqMap.put("idCardFrontBase64", getImgBase64(traceId, merchantDTO.getIdCardFrontImg()));
        reqMap.put("idCardBackBase64", getImgBase64(traceId, merchantDTO.getIdCardBackImg()));
        reqMap.put("accountName", merchantBank.getAccountName());
        reqMap.put("bankCardNo", merchantBank.getCardNo());
        reqMap.put("bankName", merchantBank.getBankName());
        reqMap.put("bankCardBase64", getImgBase64(traceId, merchantBank.getBankCardImg()));
        reqMap.put("bankProvinceCode", merchantBank.getBankProvinceCode());
        reqMap.put("bankCityCode", merchantBank.getBankCityCode());
        reqMap.put("addressProvinceCode", merchantDTO.getBusinessAddressProvinceCode());
        reqMap.put("addressCityCode", merchantDTO.getBusinessAddressCityCode());
        reqMap.put("addressAreaCode", merchantDTO.getBusinessAddressAreaCode());
        String channelPayUrl = paymentChannelDTO.getPaymentUrl();
        if (channelPayUrl.endsWith("/")) {
            channelPayUrl = channelPayUrl.substring(0, channelPayUrl.lastIndexOf("/"));
        }
        String url = channelPayUrl + RequestConfig.AYF_SPLIT_SIGN;
        String reqStr = JSONObject.toJSONString(reqMap);
        JSONObject jsonObject = JSONObject.parseObject(reqStr);
        jsonObject.remove("idCardFrontBase64");
        jsonObject.remove("idCardBackBase64");
        jsonObject.remove("bankCardBase64");
        LogPortal.info("{} 商户ID[{}] 【分账通分账-二级账户申请】请求参数[{}]", traceId, merchantDTO.getId(), jsonObject.toJSONString());
        try {
            MerchantDTO mDto = new MerchantDTO();
            mDto.setMerchantSecretKey(paymentChannelDTO.getChannelKey());
            mDto.setPrivateKey(paymentChannelDTO.getPlatformPrivateKey());
            mDto.setPublicKey(paymentChannelDTO.getChannelPublicKey());
            result = HttpUtils.doPost(traceId, url, reqStr, mDto);
            LogPortal.info("{} 商户ID[{}] 调用【分账通分账-二级账户申请】接口成功，响应报文:{}", traceId, merchantDTO.getId(), result);
        } catch (Exception e) {
            LogPortal.error("{} 商户ID[{}] 【分账通分账-二级账户申请】失败，发生了{}异常，错误描述:{}", e, traceId, merchantDTO.getId(), e.getClass().getName(), e.getMessage());
            throw new NullSetException("分账通分账-二级账户申请失败，错误描述：" + e.getMessage());
        }
        if (result == null) {
            throw new NullSetException("分账通分账-二级账户申请失败，请稍后重试！");
        }

        String responseCode = result.get(HttpUtils.RESPONSE_CODE);
        String response_data = result.get(HttpUtils.RESPONSE_DATA);
        if (!"200".equals(responseCode)) {
            LogPortal.error(" {} 商户ID[{}] 【分账通分账-二级账户申请】失败，HTTP状态码[{}] 响应报文:{}", traceId, merchantDTO.getId(), responseCode, response_data);
            throw new NullSetException("分账通分账-二级账户申请失败，请稍后重试！");
        }

        JSONObject data = null;
        try {
            data = JSON.parseObject(response_data);
        } catch (Exception e) {
            LogPortal.error("{} 商户ID[{}]【分账通分账-二级账户申请】失败，支付渠道响应内容格式错误，解析内容发生了异常:{}，响应报文:{}", e, traceId, merchantDTO.getId(), e.getMessage(), response_data);
            throw new NullSetException("分账通分账-二级账户申请失败，请稍后重试！");
        }
        String return_code = data.getString("return_code");
        if ("FAIL".equals(return_code)) {
            LogPortal.error("{} 商户ID[{}] 【分账通分账-二级账户申请】失败，失败原因:{}", traceId, merchantDTO.getId(), data.get("return_msg"));
            throw new NullSetException("分账通分账-二级账户申请失败：" + data.get("return_msg"));
        }
        String err_code = data.getString("err_code");
        if (HttpUtils.SUCCESS_CODE.equals(return_code) && StringUtils.isBlank(err_code)) {
            SignPayProductDTO payProductDTO = new SignPayProductDTO();
            payProductDTO.setChannelMerchantId(data.getString("subChannelMerchantId"));
            payProductDTO.setMerchantId(merchantDTO.getId());
            payProductDTO.setPayCode("AYF_SPLIT_PAY");
            MerchantTwoAyfDTO ayfDTO = JSONObject.parseObject(reqStr, MerchantTwoAyfDTO.class);
            ayfDTO.setIdCardBackUrl(merchantDTO.getIdCardBackImg());
            ayfDTO.setIdCardFrontUrl(merchantDTO.getIdCardFrontImg());
            ayfDTO.setBankCardUrl(merchantBank.getBankCardImg());
            payProductDTO.setMerchantTwoAyfDTO(ayfDTO);
            payProductDTO.setApplyStatus(1);
            signPayProductService.insertAyfSignPayProduct(payProductDTO);
        } else {
            String err_code_desc = data.getString("err_code_desc");
            LogPortal.error("{} 商户ID[{}] 【分账通分账-二级账户申请】失败，支付渠道错误码[{}] 错误码描述[{}]", traceId, merchantDTO.getId(), err_code, err_code_desc);
            throw new NullSetException("分账通分账-二级账户申请失败:" + err_code_desc);
        }
        return null;
    }

    @Override
    public MerchantTwoSelectResDTO merchantTwoSelect(MerchantTwoSelectReqDTO reqDTO, PaymentChannelDTO paymentChannelDTO) {
        String traceId = SnowFlakeUtils.getTraceId();
        MerchantTwoSelectResDTO res = new MerchantTwoSelectResDTO();
        res.setApplyStatus(1);
        SignPayProductDTO signPayProduct = signPayProductService.getByMerchantIdAndPayCode(reqDTO.getMerchantId(), paymentChannelDTO.getCode());

        Map<String, String> result = new HashMap<>(16);

        Map<String, Object> reqMap = new HashMap<>(16);
        reqMap.put("subChannelMerchantId", signPayProduct.getChannelMerchantId());

        String channelPayUrl = paymentChannelDTO.getPaymentUrl();
        if (channelPayUrl.endsWith("/")) {
            channelPayUrl = channelPayUrl.substring(0, channelPayUrl.lastIndexOf("/"));
        }
        String url = channelPayUrl + RequestConfig.AYF_SPLIT_GET_SIGN_STATUS;
        String reqStr = JSONObject.toJSONString(reqMap);
        LogPortal.info("{} 商户ID[{}] 【分账通分账-获取二级商户签约状态】请求参数[{}]", traceId, reqDTO.getMerchantId(), reqStr);
        try {
            MerchantDTO mDto = new MerchantDTO();
            mDto.setMerchantSecretKey(paymentChannelDTO.getChannelKey());
            mDto.setPrivateKey(paymentChannelDTO.getPlatformPrivateKey());
            mDto.setPublicKey(paymentChannelDTO.getChannelPublicKey());
            result = HttpUtils.doPost(traceId, url, reqStr, mDto);
            LogPortal.info("{} 商户ID[{}] 调用【分账通分账-获取二级商户签约状态】接口成功，响应报文:{}", traceId, reqDTO.getMerchantId(), result);
        } catch (Exception e) {
            LogPortal.error("{} 商户ID[{}] 【分账通分账-获取二级商户签约状态】失败，发生了{}异常，错误描述:{}", e, traceId, reqDTO.getMerchantId(), e.getClass().getName(), e.getMessage());
            throw new NullSetException("分账通分账-获取二级商户签约状态失败，错误描述：" + e.getMessage());
        }
        if (result == null) {
            throw new NullSetException("分账通分账-获取二级商户签约状态失败，请稍后重试！");
        }

        String responseCode = result.get(HttpUtils.RESPONSE_CODE);
        String response_data = result.get(HttpUtils.RESPONSE_DATA);
        if (!"200".equals(responseCode)) {
            LogPortal.error(" {} 商户ID[{}] 【分账通分账-获取二级商户签约状态】失败，HTTP状态码[{}] 响应报文:{}", traceId, reqDTO.getMerchantId(), responseCode, response_data);
            throw new NullSetException("分账通分账-获取二级商户签约状态失败，请稍后重试！");
        }

        JSONObject data = null;
        try {
            data = JSON.parseObject(response_data);
        } catch (Exception e) {
            LogPortal.error("{} 商户ID[{}]【分账通分账-获取二级商户签约状态】失败，支付渠道响应内容格式错误，解析内容发生了异常:{}，响应报文:{}", e, traceId, reqDTO.getMerchantId(), e.getMessage(), response_data);
            throw new NullSetException("分账通分账-获取二级商户签约状态失败，请稍后重试！");
        }
        String return_code = data.getString("return_code");
        if ("FAIL".equals(return_code)) {
            LogPortal.error("{} 商户ID[{}] 【分账通分账-获取二级商户签约状态】失败，失败原因:{}", traceId, reqDTO.getMerchantId(), data.get("return_msg"));
            throw new NullSetException("分账通分账-获取二级商户签约状态失败：" + data.get("return_msg"));
        }
        String err_code = data.getString("err_code");
        if (HttpUtils.SUCCESS_CODE.equals(return_code) && StringUtils.isBlank(err_code)) {
            SignPayProductDTO payProductDTO = new SignPayProductDTO();
            Integer status = data.getInteger("status");
            if (signPayProduct.getApplyStatus() != 2) {
                if (status == 3) {
                    payProductDTO.setApplyStatus(-1);
                    res.setApplyStatus(-1);
                } else if (status == 2) {
                    payProductDTO.setApplyStatus(2);
                    res.setApplyStatus(2);
                } else {
                    payProductDTO.setApplyStatus(1);
                }
            }
            if (signPayProduct.getUpdateStatus() == 1) {
                if (status == 3) {
                    payProductDTO.setUpdateStatus(-1);
                } else if (status == 2) {
                    payProductDTO.setUpdateStatus(2);
                } else {
                    payProductDTO.setUpdateStatus(1);
                }
            }
            if (payProductDTO.getUpdateStatus() != null || payProductDTO.getApplyStatus() != null) {
                payProductDTO.setFailMsg(data.getString("statusDesc"));
                payProductDTO.setId(signPayProduct.getId());
                signPayProductService.updateAyfSignPayProductById(payProductDTO);
            }
        } else {
            String err_code_desc = data.getString("err_code_desc");
            LogPortal.error("{} 商户ID[{}] 【分账通分账-获取二级商户签约状态】失败，支付渠道错误码[{}] 错误码描述[{}]", traceId, reqDTO.getMerchantId(), err_code, err_code_desc);
            throw new NullSetException("分账通分账-获取二级商户签约状态失败:" + err_code_desc);
        }
        return res;
    }

    @Override
    public MerchantTwoUpdateResDTO merchantTwoUpdate(MerchantTwoUpdateReqDTO reqDTO, PaymentChannelDTO paymentChannelDTO) {
        String traceId = SnowFlakeUtils.getTraceId();
        Merchant merchant = new Merchant();
        merchant.setId(reqDTO.getMerchantId());
        MerchantDTO merchantDTO = merchantService.selectOne(merchant);
        MerchantBank merchantBank = merchantBankService.getMerchantBankByMid(reqDTO.getMerchantId());
        SignPayProductDTO signPayProduct = signPayProductService.getByMerchantIdAndPayCode(merchantDTO.getId(), paymentChannelDTO.getCode());
        if (signPayProduct == null) {
            throw new ParameterException("请先进行二级账户申请");
        }
        checkParameter(merchantDTO, merchantBank);
        Map<String, String> result = new HashMap<>(16);
        Map<String, Object> reqMap = new HashMap<>(16);
        reqMap.put("subChannelMerchantId", signPayProduct.getChannelMerchantId());
        reqMap.put("customerMerchantId", merchantDTO.getId());
        reqMap.put("customerMerchantUserName", merchantBank.getAccountName());
        reqMap.put("phone", merchantBank.getBankPhone());
        reqMap.put("streetAddress", merchantDTO.getDetailAddress());
        reqMap.put("idCardStartDate", merchantDTO.getIdCardValidityTimeStart().replaceAll("-", ""));
        reqMap.put("idCardEndDate", merchantDTO.getIdCardValidityTimeEnd().replaceAll("-", ""));
        reqMap.put("idCardNo", merchantDTO.getIdCard());
        reqMap.put("idCardFrontBase64", getImgBase64(traceId, merchantDTO.getIdCardFrontImg()));
        reqMap.put("idCardBackBase64", getImgBase64(traceId, merchantDTO.getIdCardBackImg()));
        reqMap.put("accountName", merchantBank.getAccountName());
        reqMap.put("bankCardNo", merchantBank.getCardNo());
        reqMap.put("bankName", merchantBank.getBankName());
        reqMap.put("bankCardBase64", getImgBase64(traceId, merchantBank.getBankCardImg()));
        reqMap.put("bankProvinceCode", merchantBank.getBankProvinceCode());
        reqMap.put("bankCityCode", merchantBank.getBankCityCode());
        reqMap.put("addressProvinceCode", merchantDTO.getBusinessAddressProvinceCode());
        reqMap.put("addressCityCode", merchantDTO.getBusinessAddressCityCode());
        reqMap.put("addressAreaCode", merchantDTO.getBusinessAddressAreaCode());
        String channelPayUrl = paymentChannelDTO.getPaymentUrl();
        if (channelPayUrl.endsWith("/")) {
            channelPayUrl = channelPayUrl.substring(0, channelPayUrl.lastIndexOf("/"));
        }
        String url = channelPayUrl + RequestConfig.AYF_SPLIT_UPDATE_SIGN_INFO;
        String reqStr = JSONObject.toJSONString(reqMap);
        JSONObject jsonObject = JSONObject.parseObject(reqStr);
        jsonObject.remove("idCardFrontBase64");
        jsonObject.remove("idCardBackBase64");
        jsonObject.remove("bankCardBase64");
        LogPortal.info("{} 商户ID[{}] 【分账通分账-更新二级商户分账信息】请求参数[{}]", traceId, merchantDTO.getId(), jsonObject.toJSONString());
        try {
            MerchantDTO mDto = new MerchantDTO();
            mDto.setMerchantSecretKey(paymentChannelDTO.getChannelKey());
            mDto.setPrivateKey(paymentChannelDTO.getPlatformPrivateKey());
            mDto.setPublicKey(paymentChannelDTO.getChannelPublicKey());
            result = HttpUtils.doPost(traceId, url, reqStr, mDto);
            LogPortal.info("{} 商户ID[{}] 调用【分账通分账-更新二级商户分账信息】接口成功，响应报文:{}", traceId, merchantDTO.getId(), result);
        } catch (Exception e) {
            LogPortal.error("{} 商户ID[{}] 【分账通分账-更新二级商户分账信息】失败，发生了{}异常，错误描述:{}", e, traceId, merchantDTO.getId(), e.getClass().getName(), e.getMessage());
            throw new NullSetException("分账通分账-更新二级商户分账信息失败，错误描述：" + e.getMessage());
        }
        if (result == null) {
            throw new NullSetException("分账通分账-更新二级商户分账信息失败，请稍后重试！");
        }

        String responseCode = result.get(HttpUtils.RESPONSE_CODE);
        String response_data = result.get(HttpUtils.RESPONSE_DATA);
        if (!"200".equals(responseCode)) {
            LogPortal.error(" {} 商户ID[{}] 【分账通分账-更新二级商户分账信息】失败，HTTP状态码[{}] 响应报文:{}", traceId, merchantDTO.getId(), responseCode, response_data);
            throw new NullSetException("分账通分账-更新二级商户分账信息失败，请稍后重试！");
        }

        JSONObject data = null;
        try {
            data = JSON.parseObject(response_data);
        } catch (Exception e) {
            LogPortal.error("{} 商户ID[{}]【分账通分账-更新二级商户分账信息】失败，支付渠道响应内容格式错误，解析内容发生了异常:{}，响应报文:{}", e, traceId, merchantDTO.getId(), e.getMessage(), response_data);
            throw new NullSetException("分账通分账-更新二级商户分账信息失败，请稍后重试！");
        }
        String return_code = data.getString("return_code");
        if ("FAIL".equals(return_code)) {
            LogPortal.error("{} 商户ID[{}] 【分账通分账-更新二级商户分账信息】失败，失败原因:{}", traceId, merchantDTO.getId(), data.get("return_msg"));
            throw new NullSetException("分账通分账-更新二级商户分账信息失败：" + data.get("return_msg"));
        }
        String err_code = data.getString("err_code");
        if (HttpUtils.SUCCESS_CODE.equals(return_code) && StringUtils.isBlank(err_code)) {
            SignPayProductDTO payProductDTO = new SignPayProductDTO();
            payProductDTO.setId(signPayProduct.getId());
            payProductDTO.setMerchantId(merchantDTO.getId());
            payProductDTO.setUpdateStatus(1);
            payProductDTO.setFailMsg("-");
            if (signPayProduct.getApplyStatus() != null && signPayProduct.getApplyStatus() != 2) {
                payProductDTO.setApplyStatus(1);
            }
            MerchantTwoAyfDTO ayfDTO = JSONObject.parseObject(reqStr, MerchantTwoAyfDTO.class);
            ayfDTO.setIdCardBackUrl(merchantDTO.getIdCardBackImg());
            ayfDTO.setIdCardFrontUrl(merchantDTO.getIdCardFrontImg());
            ayfDTO.setBankCardUrl(merchantBank.getBankCardImg());
            payProductDTO.setApplyInfo(JSONObject.toJSONString(ayfDTO));
            signPayProductService.updateAyfSignPayProductById(payProductDTO);
        } else {
            String err_code_desc = data.getString("err_code_desc");
            LogPortal.error("{} 商户ID[{}] 【分账通分账-更新二级商户分账信息】失败，支付渠道错误码[{}] 错误码描述[{}]", traceId, merchantDTO.getId(), err_code, err_code_desc);
            throw new NullSetException("分账通分账-更新二级商户分账信息失败:" + err_code_desc);
        }
        return null;
    }

    @Override
    public MerchantTwoUpdateSuperResDTO merchantTwoUpdateSuper(MerchantTwoUpdateSuperReqDTO reqDTO, PaymentChannelDTO paymentChannelDTO) {
        String traceId = SnowFlakeUtils.getTraceId();
        Merchant merchant = new Merchant();
        merchant.setId(reqDTO.getMerchantId());
        MerchantDTO merchantDTO = merchantService.selectOne(merchant);
        Map<String, String> result = new HashMap<>(16);
        Map<String, Object> reqMap = new HashMap<>(16);
        reqMap.put("idCardNo", merchantDTO.getIdCard());
        String channelPayUrl = paymentChannelDTO.getPaymentUrl();
        if (channelPayUrl.endsWith("/")) {
            channelPayUrl = channelPayUrl.substring(0, channelPayUrl.lastIndexOf("/"));
        }
        String url = channelPayUrl + RequestConfig.AYF_SPLIT_UPDATE_SIGN_SUPER;
        String reqStr = JSONObject.toJSONString(reqMap);
        LogPortal.info("{} 商户ID[{}] 【分账通分账-解除二级商户绑定信息】请求参数[{}]", traceId, merchantDTO.getId(), reqStr);
        try {
            MerchantDTO mDto = new MerchantDTO();
            mDto.setMerchantSecretKey(paymentChannelDTO.getChannelKey());
            mDto.setPrivateKey(paymentChannelDTO.getPlatformPrivateKey());
            mDto.setPublicKey(paymentChannelDTO.getChannelPublicKey());
            result = HttpUtils.doPost(traceId, url, reqStr, mDto);
            LogPortal.info("{} 商户ID[{}] 调用【分账通分账-解除二级商户绑定信息】接口成功，响应报文:{}", traceId, merchantDTO.getId(), result);
        } catch (Exception e) {
            LogPortal.error("{} 商户ID[{}] 【分账通分账-解除二级商户绑定信息】失败，发生了{}异常，错误描述:{}", e, traceId, merchantDTO.getId(), e.getClass().getName(), e.getMessage());
            throw new NullSetException("分账通分账-解除二级商户绑定信息失败，错误描述：" + e.getMessage());
        }
        if (result == null) {
            throw new NullSetException("分账通分账-解除二级商户绑定信息失败，请稍后重试！");
        }

        String responseCode = result.get(HttpUtils.RESPONSE_CODE);
        String response_data = result.get(HttpUtils.RESPONSE_DATA);
        if (!"200".equals(responseCode)) {
            LogPortal.error(" {} 商户ID[{}] 【分账通分账-解除二级商户绑定信息】失败，HTTP状态码[{}] 响应报文:{}", traceId, merchantDTO.getId(), responseCode, response_data);
            throw new NullSetException("分账通分账-解除二级商户绑定信息失败，请稍后重试！");
        }

        JSONObject data = null;
        try {
            data = JSON.parseObject(response_data);
        } catch (Exception e) {
            LogPortal.error("{} 商户ID[{}]【分账通分账-解除二级商户绑定信息】失败，支付渠道响应内容格式错误，解析内容发生了异常:{}，响应报文:{}", e, traceId, merchantDTO.getId(), e.getMessage(), response_data);
            throw new NullSetException("分账通分账-解除二级商户绑定信息失败，请稍后重试！");
        }
        String return_code = data.getString("return_code");
        if ("FAIL".equals(return_code)) {
            LogPortal.error("{} 商户ID[{}] 【分账通分账-解除二级商户绑定信息】失败，失败原因:{}", traceId, merchantDTO.getId(), data.get("return_msg"));
            throw new NullSetException("分账通分账-解除二级商户绑定信息失败：" + data.get("return_msg"));
        }
        String err_code = data.getString("err_code");
        if (HttpUtils.SUCCESS_CODE.equals(return_code) && StringUtils.isBlank(err_code)) {
            signPayProductService.deleteByMerchantIdAndCode(reqDTO.getMerchantId(), "AYF_SPLIT_PAY");
        } else {
            String err_code_desc = data.getString("err_code_desc");
            LogPortal.error("{} 商户ID[{}] 【分账通分账-解除二级商户绑定信息】失败，支付渠道错误码[{}] 错误码描述[{}]", traceId, merchantDTO.getId(), err_code, err_code_desc);
            throw new NullSetException("分账通分账-解除二级商户绑定信息失败:" + err_code_desc);
        }
        return null;
    }

    @Override
    public boolean merchantTwoApplyAsyncExec(MerchantTwoApplyAsyncReqDTO reqDTO, PaymentChannelDTO channel) {
        return false;
    }
}
